package com.mrper.ext.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListAdapter;

import com.etsy.android.grid.StaggeredGridView;
import com.miguo.framework.view.pulltorefresh.PullToRefreshAdapterViewBase;
import com.miguo.ui.R;
import com.mrper.ext.adapter.goods.Double9Adapter;

/**
 * Created by admin on 2016/3/11.
 * 瀑布流控件
 */
public class PullToRefreshStaggeredGridView extends PullToRefreshAdapterViewBase<StaggeredGridView> {

    public PullToRefreshStaggeredGridView(Context context) {
        super(context);
    }

    public PullToRefreshStaggeredGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullToRefreshStaggeredGridView(Context context, Mode mode) {
        super(context, mode);
    }

    public PullToRefreshStaggeredGridView(Context context, Mode mode, AnimationStyle animStyle) {
        super(context, mode, animStyle);
    }

    @Override
    public Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    @Override
    protected StaggeredGridView createRefreshableView(Context context, AttributeSet attrs) {
        StaggeredGridView scrollView = new StaggeredGridView(context, attrs);
        scrollView.setId(R.id.PullToRefreshMultiColumnListView);
        return scrollView;
    }

//    @Override
//    protected boolean isReadyForPullEnd() {
//        StaggeredGridView contentView = getRefreshableView();
//        ListAdapter adapter = contentView.getAdapter();
//        if(adapter == null || adapter.getCount() == 0) return false;
//        int lastVisibleIndex = contentView.getLastVisiblePosition();
//        if(lastVisibleIndex == adapter.getCount() - 1) {
//            View lastVisibleChildView = contentView.getChildAt(lastVisibleIndex);
//            View computeView = lastVisibleChildView;
//            int maxBottom = lastVisibleChildView.getBottom();
//            if(lastVisibleIndex - 1 >= 0) {
//                View last2View = contentView.getChildAt(lastVisibleIndex - 1);
//                computeView = maxBottom > last2View.getBottom() ? lastVisibleChildView:last2View;
//            }
//            int[] location = new int[2];
//            computeView.getLocationOnScreen(location);
//            //此处的50f为TabView的定值高度，单位DIP
//            return location[1] - DensityUtil.dip2px(contentView.getContext(),50f) <= 0;
//        }
//        return false;
//    }

//    @Override
//    protected boolean isReadyForPullStart() {
//        StaggeredGridView contentView = getRefreshableView();
//        ListAdapter adapter = contentView.getAdapter();
//        return adapter!=null && (contentView.getFirstVisiblePosition() == 0 || adapter.getCount() == 0);
//    }

//    public void setAdapter(ListAdapter adapter){
//        getRefreshableView().setAdapter(adapter);
//    }
//
//    public ListAdapter getAdapter(){
//        return getRefreshableView().getAdapter();
//    }
//
    public Object getItemAtPosition(int position){
        ListAdapter adapter = getRefreshableView().getAdapter();
        if(adapter == null) return null;
        return adapter.getItem(position);
    }
//
//    public int getCount(){
//        return getAdapter() == null?0:getAdapter().getCount();
//    }

    /** 滑动大顶部 **/
    public void smoothScroll2Top(){
        StaggeredGridView contentView = getRefreshableView();
        contentView.resetToTop();
        Double9Adapter adapter = (Double9Adapter)contentView.getAdapter();
        if(adapter!=null) adapter.syncDataSource();
    }
//
    public void setSelection(int position) {
        getRefreshableView().smoothScrollToPosition(position);
    }
//
//    public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
//        getRefreshableView().setOnItemClickListener(listener);
//    }

}
